package com.japstudy.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.japstudy.entity.Grade;
import com.japstudy.service.IGradeService;
import com.japstudy.util.PageUtil;
import com.japstudy.util.UUIDUtil;

@Controller
@RequestMapping("/Grade")
public class GradeController {
	
	@Autowired
	private IGradeService gradeServiceImpl;
	
	@RequestMapping("/gradeAddUi")
	public String gradeAddUi() {
		return "admin/grade_add";
	}
	
	@RequestMapping("/gradeListUi")
	public String gradeListUi() {
		return "admin/grade_list";
	}
	
	@RequestMapping("/pageCountChange")
	public @ResponseBody List<Grade> pageCountChange(HttpServletRequest request, HttpServletResponse response, String pageCount, String nowPageNumber) {
		List<Grade> grades = (List<Grade>) request.getSession().getAttribute("realGradesAdmin");
		int intPageCount = Integer.parseInt(pageCount);
		int intNowPageNumber = Integer.parseInt(nowPageNumber);
		int maxPage = PageUtil.getMaxPage(grades, intPageCount);
		if(intNowPageNumber > maxPage) {
			intNowPageNumber = maxPage;
		}
		List<Grade> pageData = PageUtil.getPageData(grades, intNowPageNumber, intPageCount);
		return pageData;
	}
	
	@RequestMapping("/toPage")
	public @ResponseBody List<Grade> toPage(HttpServletRequest request, HttpServletResponse response, String pageCount, String toPageNumber) {
		List<Grade> grades = (List<Grade>) request.getSession().getAttribute("realGradesAdmin");
		int intPageCount = Integer.parseInt(pageCount);
		int intToPageNumber = Integer.parseInt(toPageNumber);
		List<Grade> pageData = PageUtil.getPageData(grades, intToPageNumber, intPageCount);
		return pageData;
	}
	
	@RequestMapping("/selectAll")
	public String selectAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Grade> grades = gradeServiceImpl.selectAll();
		List<Grade> pageData = PageUtil.getPageData(grades, 1, 10);
		request.getSession().setAttribute("realGradesAdmin", grades);
		request.getSession().setAttribute("gradesAdmin", pageData);
		response.sendRedirect(request.getContextPath() + "/Grade/gradeListUi");
		return null;
	}
	
	@RequestMapping("/selectByNameLike")
	public String selectByNameLike(String gradeName, HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Grade> grades = gradeServiceImpl.selectByNameLike(gradeName);
		List<Grade> pageData = PageUtil.getPageData(grades, 1, 10);
		request.getSession().setAttribute("realGradesAdmin", grades);
		request.getSession().setAttribute("gradesAdmin", pageData);
		response.sendRedirect(request.getContextPath() + "/Grade/gradeListUi");
		return null;
	}
	
	@RequestMapping("/haveSameGradeName")
	public @ResponseBody String haveSameGradeName(Grade grade) {
		Grade haveGrade = gradeServiceImpl.selectByGradeName(grade);
		if(haveGrade == null) {
			return "no";
		} else {
			return "yes";
		}
	}
	
	@RequestMapping("/gradeAdd")
	public String gradeAdd(Grade grade, HttpServletRequest request, HttpServletResponse response) throws IOException {
		grade.setGradeId(UUIDUtil.getUUID());
		if(1 == gradeServiceImpl.add(grade)) {
			response.sendRedirect(request.getContextPath() + "/Grade/selectAll");
			return null;
		} else {
			return null;
		}
	}
	
	@RequestMapping("/gradeUpdateUi")
	public ModelAndView gradeUpdateUi(String gradeId){
		Grade updateGrade = gradeServiceImpl.selectById(gradeId);
		ModelAndView mav = new ModelAndView("admin/grade_update", "updateGradeAdmin", updateGrade);
		return mav;
	}
	
	@RequestMapping("/gradeUpdate")
	public String gradeUpdate(Grade grade, HttpServletRequest request, HttpServletResponse response) throws IOException {
		int result = gradeServiceImpl.update(grade);
		if(1 == result) {
			response.sendRedirect(request.getContextPath() + "/Grade/selectAll");
		} else {
			response.sendRedirect(request.getContextPath() + "/Grade/gradeUpdateUi?gradeId=" + grade.getGradeId());
		}
		return null;
	}
	
	@RequestMapping("/delete")
	public String delete(Grade grade, HttpServletRequest request, HttpServletResponse response) throws IOException {
		gradeServiceImpl.delete(grade);
		response.sendRedirect(request.getContextPath() + "/Grade/selectAll");
		return null;
	}
}
